package core;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestPersonAccount {

	PersonAccount account = null;

	@Before
	public void setUp() throws Exception {
		account = new PersonAccount(100);
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testBuyGold() {
		account.updateGoods(10, 9);
		account.buy(1);
		assertEquals(90, account.getAvailableMoney(), 0.001);
		assertEquals(99, account.getAsset(), 0.01);
		assertEquals(9 / 99.0, account.getGoodsRate(), 0.001);

		account.updateGoods(11, 10);
		assertEquals(90, account.getAvailableMoney(), 0.001);
		assertEquals(100, account.getAsset(), 0.01);
		assertEquals(10 / 100.0, account.getGoodsRate(), 0.001);

		account.sell(1);
		assertEquals(100, account.getAvailableMoney(), 0.001);
		assertEquals(100, account.getAsset(), 0.01);
		assertEquals(0, account.getGoodsRate(), 0.001);

		account.sell(1);
		assertEquals(88, account.getAvailableMoney(), 0.001);
		assertEquals(99, account.getAsset(), 0.01);
		assertEquals(11 / 99.0, account.getGoodsRate(), 0.001);
	}

	@Test
	public void testSellGold() {
		account.updateGoods(10, 9);
		account.sell(1);
		assertEquals(89, account.getAvailableMoney(), 0.001);
		assertEquals(99, account.getAsset(), 0.001);
		assertEquals(10 / 99.0, account.getGoodsRate(), 0.001);

		account.updateGoods(9, 8);
		assertEquals(91, account.getAvailableMoney(), 0.001);
		assertEquals(100, account.getAsset(), 0.001);
		assertEquals(9 / 100.0, account.getGoodsRate(), 0.001);

		account.updateGoods(12, 11);
		assertEquals(85, account.getAvailableMoney(), 0.001);
		assertEquals(97, account.getAsset(), 0.001);
		assertEquals(12 / 97.0, account.getGoodsRate(), 0.001);

		account.buy(1);
		assertEquals(97, account.getAvailableMoney(), 0.001);
		assertEquals(97, account.getAsset(), 0.001);
		assertEquals(0, account.getGoodsRate(), 0.001);

		account.buy(1);
		assertEquals(85, account.getAvailableMoney(), 0.001);
		assertEquals(96, account.getAsset(), 0.001);
	}
}
